package com.cumt.threeway.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cumt.threeway.R;
import com.cumt.threeway.model.Activities;

import java.util.List;

/**
 * Created by 胡晶晶 on 2016/4/8.
 */
public class BaseFragmentAdapter extends RecyclerView.Adapter{

    public static interface OnRecycleViewListener {
        public void OnRecycleViewItemClick(int position);
    }

    private OnRecycleViewListener mOnRecycleViewListener;
    private List<Activities> mList;
    private Context mContext;

    public BaseFragmentAdapter(Context context, List<Activities> list) {
        mContext = context;
        mList = list;
    }

    public void setOnRecycleViewListener(OnRecycleViewListener onRecycleViewListener) {
        this.mOnRecycleViewListener = onRecycleViewListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycleview_item_layout, null, false);
        return new ActivitiesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ActivitiesViewHolder activitiesViewHolder = (ActivitiesViewHolder) holder;
        Activities activities = mList.get(position);
        activitiesViewHolder.mTitleTextView.setText(activities.getTitle());
        activitiesViewHolder.mContentTextView.setText(activities.getContent());
        activitiesViewHolder.mDateTextView.setText(activities.getDate());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ActivitiesViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{

        private ImageView mImageView;
        private TextView mTitleTextView;
        private TextView mContentTextView;
        private TextView mDateTextView;
        private View mLineLayout;

        public ActivitiesViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.iv_icon);
            mTitleTextView = (TextView) itemView.findViewById(R.id.tv_title);
            mContentTextView = (TextView) itemView.findViewById(R.id.tv_site);
            mDateTextView = (TextView) itemView.findViewById(R.id.tv_date);
            mLineLayout = itemView.findViewById(R.id.ll_parent);
            mLineLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mOnRecycleViewListener.OnRecycleViewItemClick(this.getPosition());
        }
    }
}
